﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;
using Hisun.Models;

namespace Hisun.Controllers
{
    /// <summary>
    /// 产品中间体操作类
    /// Copyright (C) 2008 HOHO``
    /// All rights reserved
    /// NameSpace:Hisun.Controllers.ProductActivityProccess
    /// Description :
    /// Created By QiuZhongJie At 2008/11/3 18:11
    /// </summary>
    public class AbcProductActivityProccess
    {
        private HisunDBDataContext db = new HisunDBDataContext();
        private string lastError;

        public string LastError
        {
            get { return lastError; }
            set { lastError = value; }
        }
        /// <summary>
        /// 根据作业中心ID获得中间体的数据库条目
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public IQueryable<TABC_JS_ProductInProcess> GetProductInProcessesByCostCenter(int id)
        {
            //if (db.TABC_JS_ProductInProcess.Count(p => p.作业中心ID == id) == 0)
            //    return null;
            //else
            return (from p in db.TABC_JS_ProductInProcess
                    where p.作业中心ID == id
                    select p);
        }

        /// <summary>
        /// 根据中间体ID获得中间体的数据库条目
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public TABC_JS_ProductInProcess GetProductInProcess(int id)
        {
            var retval =  (from p in db.TABC_JS_ProductInProcess
                    where p.ID == id
                    select p).Single();
            SafeCheck(retval.作业中心ID.Value);
            return retval;
        }

        /// <summary>
        /// 更新现有的中间体
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        public int UpdateProductInProcess(TABC_JS_ProductInProcess p)
        {
            if (p == null)
                throw new Exception("没有有效的更新数据传入!");
            var oldP = (from c in db.TABC_JS_ProductInProcess
                        where c.ID == p.ID
                        select c).Single();
            oldP = AutoField(p);//更新自动列
            db.SubmitChanges(ConflictMode.ContinueOnConflict);
            return 0;
        }

        /// <summary>
        /// 添加新的中间体
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        public int AddNewProductInProcess(TABC_JS_ProductInProcess p)
        {
            if (p == null)
                throw new Exception("没有有效的新增数据插入!");
            p = AutoField(p);//更新自动列
            db.TABC_JS_ProductInProcess.InsertOnSubmit(p);
            db.SubmitChanges(ConflictMode.ContinueOnConflict);
            return 0;
        }

        /// <summary>
        /// 使用带有中间体ID冲突检查的方式添加新的中间体
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns>添加前存在中间体ID时抛出异常</returns>
        public int AddNewProductInProcessWithSafeCheck(TABC_JS_ProductInProcess p)
        {
            if (p == null)
                throw new Exception("没有有效的新增数据插入!");
            p = AutoField(p);//更新自动列
            if (db.TABC_JS_ProductInProcess.Count(c => c.中间体ID == p.中间体ID) != 0)
                throw new Exception("已经有定义该中间体!请从选择更新的方式来修改这条数据!");
            return AddNewProductInProcess(p);
        }


        /// <summary>
        /// 返回中间体类型数据源
        /// </summary>
        /// <returns></returns>
        public IQueryable<TABC_DM_ProductInProcessClass> GetProductInProcessClass()
        {
            return (from c in db.TABC_DM_ProductInProcessClasses
                    select c);
        }


        /// <summary>
        /// 自动补全剩余的字段 作业中心安全检测 返回新的PIP
        /// </summary>
        /// <param name="pip">The pip.</param>
        /// <returns></returns>
        public TABC_JS_ProductInProcess AutoField(TABC_JS_ProductInProcess pip)
        {
            SafeCheck(pip.作业中心ID.Value);
            var product = (from p in db.TABC_DM_Product
                           where p.产品ID == pip.产品ID
                           select p).Single();
            pip.计量单位 = product.计量单位;
            pip.中间体ID = pip.作业中心ID.Value.ToString() + pip.产品ID;
            pip.操作员ID = HttpContext.Current.Session[ServerVars.sysSessionUserName].ToString();
            return pip;
        }

        public void SafeCheck(int RecordcostCenterID)
        {
            AbcAccount aa = new AbcAccount(HttpContext.Current.Session[ServerVars.sysSessionUserName].ToString());
            int costCenterID = RecordcostCenterID;
            int userCostCenterID = aa.GetCostCenterID();
            while (true)
            {
                if (costCenterID != userCostCenterID)
                {
                    try
                    {
                        costCenterID = (from c in db.TABC_JS_CostCenter
                                        where c.作业中心ID == costCenterID
                                        select c.上级作业中心ID).Single().Value;
                    }
                    catch (Exception ex)
                    {
                        lastError = ex.Message;
                        throw new Exception("非法操作!你没有操作这个记录的权限!");
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}
